<?php
//命名空间
namespace Shop\Controller;
//使用命名空间 引入空间里面的类
use Think\Model;

use Think\Controller;

class CartController extends BaseController {
	
	private $userModel;
	private $productModel;
	private $order;
	private $userInfo;
	private $nologin=array('loginOut');
	public  function  __construct()
	{
		parent::__construct();
		$this->userModel=M('user');
		$this->productModel=M("product");
		$this->order=M("order");
		$this->userInfo=session("userinfo");
		
	}
	
	public function index(){
		/**$cartArray=$this->order->join("order_n_product ON order_n_product.order_id= order.id")->select();
		$proids=array();
		foreach($cartArray as $value)
		{
			$proids[]=$value['product_id'];
		}**/
		
		$orderArray=cookie("order_product");
		$orderInfo=array();
		$productids="";
		foreach($orderArray as $orvalue)
		{
			$infoArray=explode("|", $orvalue);
			$orderInfo[$infoArray[0]]=$infoArray[1];
			$productids .=$infoArray[0].",";
		}
		$productids=substr_replace($productids,'',-1,1);
		$productArray=$this->productModel->where("id in(".$productids.")")->select();
		$cartArray=array();
		foreach($orderInfo as $orkey=>$orvalue)
		{
			foreach ($productArray as $prokey=>$provalue)
			{
				if($orkey==$provalue['id'])
				{
					$cartArray[$prokey]['image']=$provalue['pic'];
					$cartArray[$prokey]['productid']=$provalue['id'];
					$cartArray[$prokey]['pname']=$provalue['name'];
					$cartArray[$prokey]['shop_price']=$provalue['shop_price'];
					$cartArray[$prokey]['number']=$orvalue;
					$cartArray[$prokey]['price']=$provalue['shop_price']*$orvalue;
				}
			}
		
		}
		$this->assign("cartArray",$cartArray);
		$this->view();
	
	}
	
	public function manage()
	{
		if(IS_AJAX)
		{
			$productid=I('get.productid');
			$number=I('get.number');
			$sign=I('get.sign');
			$do=I('get.do');
			$orproduct=cookie("order_product");
			if($do=='update')
			{
				foreach($orproduct as $orkey=>$orvalue)
				{
					$valueArray=array();
					$valueArray=explode("|",$orvalue);
					if($valueArray[0]==$productid)
					{
						$valueArray[1]=$number;
					}
					$orvalue2=join("|",$valueArray);
					$orproduct[$orkey]=$orvalue2;
				}
				cookie("order_product",$orproduct,array('expire'=>C("CART_LIFETIME")));
				$this->ajaxReturn('su');
			}
			else if($do=='delete')
			{
				foreach ($orproduct as $orkey=>$orvalue)
				{
					if($sign==$orvalue)
					{
						unset($orproduct[$orkey]);
					}
				}
				cookie("order_product",$orproduct,array('expire'=>C("CART_LIFETIME")));
				$this->ajaxReturn('su');
			}
			
		}
		
	
	}
	
	public function addCart()
	{
		
			if(IS_AJAX){
				$proid=I('get.proid');
				$pronum=I('get.pronum');
			
			/**$productInfo=$this->productModel->where("id=".$proid)->find();
			
			$orderArray=array(
				'ctime'=>time(),
				'price'=>$productInfo['shop_price']*$pronum,
				'status'=>0,
				'content'=>'ee',
				'user_address_id'=>0,
				'user_id'=>$userInfo['id']?$userInfo['id']:0,
			);
			$orderid=$this->order->data($orderArray)->add();
			$prorder=array(
				'order_id'=>$orderid,
				'product_id'=>$proid,
				'number'=>$pronum
			);
			$porder=M('order_n_product')->data($prorder)->add();
			**/
				$orderArrayB=array();
				$orderStr=$proid."|".$pronum;
				//$orderArrayB=cookie("order_product");
				if(is_array(cookie("order_product")))
				{
					$orderArrayB=cookie("order_product");
				}
				$orderArrayB[]=$orderStr;
				cookie("order_product",$orderArrayB,array('expire'=>C("CART_LIFETIME")));
				$this->ajaxReturn("购物车添加成功",'json');
			}
	}
	
}